from abc import ABC, abstractmethod
from .base_card import BasicCard
from typing import Dict, Any, Optional


class Champion(BasicCard):
    """Champion 基类"""
    
    def __init__(self, card_id: str, name: str, image_path: str, description: str, 
                 cost: int, attack: int, hp: int):
        super().__init__(card_id, name, image_path, description)
        self.cost = cost
        self.attack = attack
        self.hp = hp
        self.can_attack_this_turn = False
        self.summoned_this_turn = True
        self.position = None  # 在场上的位置 (0-4)
    
    def to_dict(self) -> Dict[str, Any]:
        """将 Champion 序列化为字典"""
        data = super().to_dict()
        data.update({
            "type": "Champion",  # 强制设置为Champion，而不是子类名
            "cost": self.cost,
            "attack": self.attack,
            "hp": self.hp,
            "can_attack_this_turn": self.can_attack_this_turn,
            "summoned_this_turn": self.summoned_this_turn,
            "position": self.position
        })
        return data
    
    def take_damage(self, damage: int) -> bool:
        """受到伤害，返回是否死亡"""
        self.hp = max(0, self.hp - damage)
        return self.hp <= 0
    
    def heal(self, amount: int):
        """恢复生命值（不设上限）"""
        self.hp += amount
    
    def is_alive(self) -> bool:
        """是否存活"""
        return self.hp > 0
    
    def can_attack(self) -> bool:
        """是否可以攻击"""
        return self.is_alive() and self.can_attack_this_turn and not self.summoned_this_turn
    
    def start_new_turn(self):
        """新回合开始时的处理"""
        self.summoned_this_turn = False
        self.can_attack_this_turn = True
    
    def on_summon(self, game_state, player_id: str):
        """入场效果钩子 - 子类可重写"""
        pass
    
    def on_death(self, game_state, player_id: str):
        """死亡效果钩子 - 子类可重写"""
        pass
    
    def on_deal_damage(self, game_state, target, damage: int, player_id: str):
        """造成伤害后触发钩子 - 子类可重写"""
        pass


class Warrior(Champion):
    """战士 - 基础类型，无特殊效果"""
    
    def __init__(self, card_id: str, name: str, image_path: str, description: str, 
                 cost: int, attack: int, hp: int):
        super().__init__(card_id, name, image_path, description, cost, attack, hp)


class Guard(Champion):
    """守卫 - 可以守卫友方单位"""
    
    def __init__(self, card_id: str, name: str, image_path: str, description: str, 
                 cost: int, attack: int, hp: int):
        super().__init__(card_id, name, image_path, description, cost, attack, hp)
        self.guarding_target_id = None  # 被守卫的目标ID
    
    def to_dict(self) -> Dict[str, Any]:
        """将 Guard 序列化为字典"""
        data = super().to_dict()
        data["guarding_target_id"] = self.guarding_target_id
        return data
    
    def set_guard(self, target_id: str):
        """设置守卫目标"""
        self.guarding_target_id = target_id
    
    def is_guarding(self, target_id: str) -> bool:
        """是否在守卫指定目标"""
        return self.guarding_target_id == target_id


class Mage(Champion):
    """法师 - 主动攻击时不受反击伤害"""
    
    def __init__(self, card_id: str, name: str, image_path: str, description: str, 
                 cost: int, attack: int, hp: int):
        super().__init__(card_id, name, image_path, description, cost, attack, hp)
        self.is_mage = True
    
    def to_dict(self) -> Dict[str, Any]:
        """将 Mage 序列化为字典"""
        data = super().to_dict()
        data["is_mage"] = self.is_mage
        return data


class Assassin(Champion):
    """刺客 - 首次攻击前隐身，无法被选为目标"""
    
    def __init__(self, card_id: str, name: str, image_path: str, description: str, 
                 cost: int, attack: int, hp: int):
        super().__init__(card_id, name, image_path, description, cost, attack, hp)
        self.is_hidden = True
        self.has_attacked = False
    
    def to_dict(self) -> Dict[str, Any]:
        """将 Assassin 序列化为字典"""
        data = super().to_dict()
        data.update({
            "is_hidden": self.is_hidden,
            "has_attacked": self.has_attacked
        })
        return data
    
    def attack_target(self):
        """攻击目标后解除隐身"""
        self.has_attacked = True
        self.is_hidden = False


class Rider(Champion):
    """骑士 - 召唤的回合即可攻击"""
    
    def __init__(self, card_id: str, name: str, image_path: str, description: str, 
                 cost: int, attack: int, hp: int):
        super().__init__(card_id, name, image_path, description, cost, attack, hp)
        self.summoned_this_turn = False  # Rider 召唤后即可攻击
        self.can_attack_this_turn = True
    
    def start_new_turn(self):
        """新回合开始时的处理 - Rider重写以保持攻击能力"""
        self.summoned_this_turn = False
        self.can_attack_this_turn = True
    
    def to_dict(self) -> Dict[str, Any]:
        """将 Rider 序列化为字典"""
        data = super().to_dict()
        return data
